<script setup lang="ts">
import { ref } from "vue";
import tree from "./tree.vue";
import { useLogSetting } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import SettingIcon from "@iconify-icons/ri/settings-5-fill";
import {
  PlusDrawerForm
} from "plus-pro-components";
defineOptions({
  name: "logsSetting"
});

const treeRef = ref();
const tableRef = ref();

const {
  loading,
  tableColumns,
  dataList,
  treeData,
  treeLoading,
  pagination,
  deviceDetection,
  onSearch,
  openDialog,
  onTreeSelect,
  handleSizeChange,
  handleCurrentChange,
  settingVisible,
  columns,
  settingInfo,
  handleSaveSetting
} = useLogSetting(tableRef, treeRef);
</script>

<template>
  <div :class="['flex', 'justify-between', deviceDetection() && 'flex-wrap']">
    <tree 
    ref="treeRef" 
    :class="['mr-2', deviceDetection() ? 'w-full' : 'min-w-[200px]']" 
    :treeData="treeData"
    :treeLoading="treeLoading" 
    @tree-select-call="onTreeSelect" />
    <div
      :class="[deviceDetection() ? ['w-full', 'mt-2'] : 'w-[calc(100%-200px)]']"
    >
      <PureTableBar
        title="应用清单"
        :columns="tableColumns"
        @refresh="onSearch"
      >
        <template v-slot="{ size, dynamicColumns }">          
          <pure-table
            ref="tableRef"
            row-key="id"
            adaptive
            :adaptiveConfig="{ offsetBottom: 108 }"
            align-whole="center"
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(SettingIcon)"
                @click="openDialog(row)"
              >
                日志配置
              </el-button>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
    <div>
      <PlusDrawerForm 
      v-model:visible="settingVisible" 
      v-model="settingInfo" 
      :form="{ columns }"
      class="plusDrawer" 
      cancel-text="关闭" 
      confirm-text="保存"
      @confirm="handleSaveSetting" 
      title="日志采集配置" />
    </div>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-button:focus-visible) {
  outline: none;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
